import { UserInfo } from "@/users";
import { String } from "@/utils/string";
import { Result } from "../projects";
import axios from "axios";
import { Part } from "../projects";

const url = process.env.DEVICE_OPS_SERVICE_BASE_URL;

export async function getPartsByProjectAsync(
  userInfo: UserInfo,
  projectId: string
) {
  const response = await axios.get(
    url + `/api/device-maintenance/parts/by-project-id/${projectId}`,
    {
      headers: {
        Authorization:
          userInfo.tokenInfo.token_type + " " + userInfo.tokenInfo.access_token,
      },
    }
  );
  const result = response.data as unknown as Result<Part[]>;

  if (result.isSuccess) {
    return result.result.filter((item) =>
      String.isNullOrWhitespace(item.partOwnerId)
    );
  } else {
    throw result.message;
  }
}
